package org.doc.config;

import cn.hutool.core.io.resource.ClassPathResource;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry;

import java.io.File;
import java.io.IOException;

public interface ChineseFont {
    IFontProvider CHINESE_FONT_PROVIDER = (familyName, encoding, size, style, color) -> {
        try {
            ClassPathResource classPathResource = new ClassPathResource("font" + File.separator + "PingFangChangAnTi-2.ttf");
            System.out.println(classPathResource.getPath());
            BaseFont bf = BaseFont.createFont(classPathResource.getPath(), BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);
            Font font = new Font(bf, size, style, color);
            if(familyName != null) {
                font.setFamily(familyName);
            }
            return font;
        } catch(DocumentException | IOException e) {
            e.printStackTrace();
            return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
        }
    };
}
